#include "path_selector.hpp"

#include <QLayout>
#include <QFileDialog>

namespace FRAMEWORK {

/*****************************************************************************/

PathSelector::PathSelector ( QWidget *xParent )
  : QFrame ( xParent ) {

  setFrameShape  ( QFrame::StyledPanel ) ;
  setFrameShadow ( QFrame::Sunken      ) ;

  QVBoxLayout *M = new QVBoxLayout( this ) ;
  M->setSpacing ( 0 ) ;
  M->setMargin  ( 6 ) ;

  {
    QLabel *C = lblCaption = new QLabel( this ) ;
    C->setText( tr("Caption") ) ;
    M->addWidget( C ) ;
  }{
    QHBoxLayout *H = new QHBoxLayout ;
    H->setMargin  ( 0 ) ;
    H->setSpacing ( 3 ) ;
    M->addLayout( H ) ;
    {
      QLineEdit *P = edtPath = new QLineEdit ( this ) ;
      P->setMinimumWidth( 300 ) ;
      H->addWidget( P ) ;
    }{
      QToolButton *B = btnBrowse = new QToolButton ( this ) ;
      B->setText( tr("...") ) ;
      connect ( B, SIGNAL(clicked()), this, SLOT(browse()) ) ;
      H->addWidget( B ) ;
    }
  }

}

/*****************************************************************************/

PathSelector::~PathSelector() {
}

/*****************************************************************************/

QString PathSelector::getPath () const {
  QString Result = edtPath->text().simplified() ;
  if ( Result.isEmpty() ) return QString() ;
  return Result ;
}

/*****************************************************************************/

QString PathSelector::getCaption () const {
  QString Result = lblCaption->text().simplified() ;
  if ( Result.isEmpty() ) return QString() ;
  return Result ;
}

/*****************************************************************************/

void PathSelector::setPath ( const QString & xPath ) {
  edtPath->setText( xPath.simplified() ) ;
}

/*****************************************************************************/

void PathSelector::setCaption ( const QString & xCaption ) {
  lblCaption->setText( xCaption.simplified() ) ;
}

/*****************************************************************************/

void PathSelector::browse() {

  QString Dir = QFileDialog::getExistingDirectory(
					this,
					tr("Select directory"),
					getPath(),
					QFileDialog::ShowDirsOnly
					| QFileDialog::DontResolveSymlinks) ;

  setPath( Dir ) ;

}

/*****************************************************************************/

} // namespace FRAMEWORK

